package com.alliky.user.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alliky.core.base.BaseFragment;
import com.alliky.core.bus.RxBus;
import com.alliky.core.entity.vo.UserVO;
import com.alliky.core.router.RoutePath;
import com.alliky.user.BR;
import com.alliky.user.R;
import com.alliky.user.databinding.FragmentMyBinding;
import com.alliky.user.ui.viewmodel.MyViewModel;

@Route(path = RoutePath.User.FRAGMENT_MY)
public class MyFragment extends BaseFragment<FragmentMyBinding, MyViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_my;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        //通过binding拿到toolbar控件, 设置给Activity
        ((AppCompatActivity) getActivity()).setSupportActionBar(binding.include.toolbar);

        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(manager);

        //初始化标题
        mViewModel.initToolbar();
        mViewModel.initData();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        mViewModel.uc.finishRefresh.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                binding.refreshLayout.finishRefresh();
            }
        });

        mViewModel.uc.finishLoadMore.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                binding.refreshLayout.finishLoadMore();
            }
        });

        RxBus.getDefault().getStickyEvent(UserVO.class);
    }
}
